const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const check = require('../../utils/check.js');
const app = getApp();
const rules = {
  nickName: '姓名',
  mobile: '联系方式',
}
Page({
  data: {
    title: '会员请完善个人信息后，才能进行会费缴纳',
    nickName: "", 
    mobileError: "", 
    timerId: null, 
    mobile: "", 
    id: "",
    timerId: null
  },
  //电话校验格式
  phoneCheck(e) {
    let results = check.isValidPhone(e.detail)
    this.setData({
      mobileError: results
    })
  },
  
  onShow() {
    const that = this 
    // if (wx.getStorageSync('userInfo').phone ) {
      util.request(api.userInfoDetail, { id: that.data.id }, 'get').then(res => {
        if (res.errno === 200) { 
          if (res.data.mobile != "") {
            that.setData({
              nickName: res.data.nickName,
              mobile: res.data.mobile,
            })
          }else{
            that.setData({
              nickName:'',
              mobile: '',
            })
          }
        } 
      })
    // }
  },
  onLoad(options) {
    const that = this;
    if (options.title) {
      that.setData({
        title: options.title
      })
    }
    // if (wx.getStorageSync('userInfo').phone != "") {
    //   that.setData({
    //     mobile: wx.getStorageSync('userInfo').phone,
    //     nickName: wx.getStorageSync('userInfo').nickName
    //   })
    // }
    that.setData({
      id: wx.getStorageSync('userInfo').id,
    })
    util.request(api.userInfoDetail, { id: that.data.id }, 'get').then(res => {
      if (res.errno === 200) { 
        if (res.data.mobile != "") {
          that.setData({
            nickName: res.data.nickName,
            mobile: res.data.mobile,
          })
        }else{
          that.setData({
            nickName:'',
            mobile: '',
          })
        }
      } 
    })

  },
  clearTimer: function () {
    const timerId = this.data.timerId;
    if (timerId !== null) {
      clearTimeout(timerId);
      this.setData({
        timerId: null,
      });
    }
  },

  // 页面卸载时清除定时器
  onUnload: function () {
    this.clearTimer();
  },
  onSubmit() {
    const { mobile, nickName, id } = this.data
    let infoUser = { mobile, nickName, id }
    let hasError = false
    let keys = Object.keys(infoUser);
    for (let i = 0; i < keys.length; i++) {
      let message = this.validate(null, keys[i])
      if (message) {
        hasError = true;
      }
    }
    if (mobile == "" || mobile.length != 11) {
      wx.showToast({
        title: '手机号不完整',
        icon: 'error',
        duration: 2000
      })
      return;

    }

    if (nickName.length <= 1) {
      wx.showToast({
        title: '姓名不完整',
        icon: 'error',
        duration: 2000
      })
      return;
    }
    this.requestApi(infoUser)
  },
  //校验，既要不为空，switch也要内容格式正确
  validate(e, objKey) {
    let { key } = (e && e.target.dataset) || {}
    let curKey = objKey || key;
    if (!rules.hasOwnProperty(curKey)) return ''
    let curVal = this.data[curKey];
    let message = '';
    if (!curVal) {
      this.setData({
        [curKey + 'Error']: rules[curKey] + '不可为空',
      })
      message = rules[curKey] + '不可为空';
    } else {
      this.setData({
        [curKey + 'Error']: '',
      })
      message = ''
    }
    switch (objKey) {
      case 'mobile':
        message = check.isValidPhone(curVal)
        this.setData({
          phoneError: message
        })
        break;
    }
    return message;
  },
  requestApi(infoUser) {
    util.request(api.userInfoUpdate, infoUser, 'post').then(res => {
      if (res.errno === 200) {
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 5000
        })
        wx.setStorageSync('mobile', this.data.mobile)
        wx.setStorageSync('nickName', this.data.nickName)

        let storedUserInfo = wx.getStorageSync('userInfo');
        storedUserInfo.isMember = res.errmsg;
        wx.setStorageSync('userInfo', storedUserInfo);
        // console.log("赋值之后的值"+wx.getStorageSync('userInfo').isMember)

        wx.switchTab({ url: "/pages/Mine/Mine" })
      } else {
        wx.showToast({
          title: res.errmsg,
          icon: 'error',
          duration: 2000
        })
      }
    }).catch(e => {
    })
  }
});
